<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>apache 禁止指定 user_agent | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>apache 禁止指定 user_agent</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;user_agent 把它叫做浏览器标识，目前主流的浏览器有 IE、chrome、firefox、360、iphone上的 safari、android手机上的 百度搜索引擎、google搜索引擎等很多，每一种浏览器都有对应的 user_agent 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;常见 user_agent</p>
<blockquote>
<p>   Opera<br>    Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/39.0.2171.95 Safari/537.36 OPR/26.0.1656.60<br>    Opera/8.0 (Windows NT 5.1; U; en)<br>    Mozilla/5.0 (Windows NT 5.1; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.50<br>    Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.50        </p>
<p>   Firefox<br>    Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0<br>    Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922      Ubuntu/10.10 (maverick) Firefox/3.6.10        </p>
<p>   Safari<br>    Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2         </p>
<p>   chrome<br>    Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)  Chrome/39.0.2171.71 Safari/537.36<br>    Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11<br>    Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16        </p>
<p>   360<br>    Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36<br>    Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko</p>
</blockquote>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面针对这些 user_agent 来做一些限制。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># vim /usr/local/apache2/conf/extra/httpd-vhosts.conf</span></div><div class="line"></div><div class="line"></div><div class="line">&lt;IfModule mod_rewrite.c&gt;</div><div class="line">    RewriteEngine on</div><div class="line">    RewriteCond %&#123;HTTP_USER_AGENT&#125; ^.*curl.* [NC,OR]</div><div class="line">    RewriteCond %&#123;HTTP_USER_AGENT&#125; ^.*chrome* [NC]</div><div class="line">    RewriteRule .* - [F]</div><div class="line">&lt;/IfModule&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;同样是使用 rewrite 模块来实现限制指定 user_agent ，在上例中， RewriteRule .<em> -[F] 可以直接禁止访问， rewritecond 用 user_agent 来匹配，</em>curl<em> 表示，只要 user_agent 中含有 curl 就符合条件，其中 </em> 表示任意字符， NC 表示不区分大小写，OR 表示或者，连接下一个以条件。这样就把 curl 和 chrome 浏览器禁止掉了。假如现在要把百度的搜索引擎限制掉，可以加这样一条规则：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">RewriteCond %&#123;HTTP_USER_AGENT&#125; ^*Baiduspider/2.0* [NC]</div><div class="line"> RewriteRule .* - [F]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;既然后了或者 OR，那有没有并且呢。只要不写 OR 就是并且的意思</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Apache/53. apache 禁止指定 user_agent/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
